package com.guigu.servicesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicesystem.entity.TbMenu;
import com.guigu.servicesystem.entity.TbResource;
import com.guigu.servicesystem.entity.vo.Resource_menuVo;
import com.guigu.servicesystem.service.TbMenuService;
import com.guigu.servicesystem.service.TbResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-09
 */
@RestController
@RequestMapping("/servicesystem/resource")
public class TbResourceController {
    //注入
    @Autowired
    private TbResourceService tbResourceService;
    @Autowired
    private TbMenuService menuService;

    //查询所有
    @GetMapping("findAll")
    public Result findAllTbResource(){
        List<TbResource> list = tbResourceService.list(null);
        return  Result.ok().data("items",list);
    }

    //条件+分页查询数据
    @PostMapping("findPage")
    public Result findPage(@RequestBody Map<String, Object> searchMap,Integer page, Integer size) {
        return tbResourceService.findPage(searchMap, page, size);
    }

    //根据id查询
    @GetMapping("findById")
    public Result getTbResource(String id) {
        TbResource tbResource = tbResourceService.getById(id);
        return Result.ok().data("tbResource",tbResource);
    }

    //添加接口的方法
    @PostMapping("add")
    public Result addTbResource(@RequestBody TbResource tbResource) {
        boolean save = tbResourceService.save(tbResource);
        if(save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //修改功能
    @PostMapping("update")
    public Result updateTbResource(@RequestBody TbResource tbResource){
        boolean save = tbResourceService.updateById(tbResource);
        if(save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //逻辑删除
    @DeleteMapping("delete")
    public Result removeTbResource(Integer id) {
        boolean flag = tbResourceService.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //查找角色对应的权限
    @GetMapping("selectTbResource")
    public Result selectTbResource(int id){

        return null;
    }
//    查找所有的权限跟菜单
    @GetMapping("selectAllResource")
    public Result selectAllResource(){
        List<TbResource> tbResource= tbResourceService.list(null);
        List<TbMenu> tbMenus=menuService.list(null);
        Map map=new HashMap();
        map.put("resource",tbResource);
        map.put("menu",tbMenus);
        return Result.ok().data("resourcelist",map);
    }
}

